package com.ed.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ed.domain.Response;
import com.ed.domain.ResponseDetails;
import com.ed.dto.PageAnalysisDto;
import com.ed.mapper.IQuestionAnalysisMapper;
import com.ed.mapper.IResponseDetailsMapper;
import com.ed.mapper.IResponseMapper;
import com.ed.result.PageResult;
import com.ed.service.QuestionAnalysisService;
import com.ed.utils.TimeUtil;
import com.ed.vo.PageAnalysisVO;
import com.ed.vo.QuestionAnalysisVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class QuestionAnalysisImpl implements QuestionAnalysisService {
    @Autowired
    private IResponseDetailsMapper responseDetailsMapper;
    @Autowired
    private IResponseMapper responseMapper;
    @Autowired
    IQuestionAnalysisMapper questionAnalysisMapper;
    @Override
    public QuestionAnalysisVO listData() {
        QuestionAnalysisVO questionAnalysisVO = new QuestionAnalysisVO();
        //1.查询总答题数
        Long questionCount = responseDetailsMapper.selectCount(new QueryWrapper<>());
        questionAnalysisVO.setQuestionCount(questionCount.intValue());

        //2.查询已完成答题数
        //查询 status 字段为空的记录数量
        QueryWrapper<ResponseDetails> nullStatusQueryWrapper = new QueryWrapper<>();
        nullStatusQueryWrapper.isNull("user_answer");
        Long nullStatusCount = responseDetailsMapper.selectCount(nullStatusQueryWrapper);
        // 计算已完成的问题数量
        Long finishedCount = questionCount - nullStatusCount;
        //3.计算已完成答题率
        Double finishedRate = finishedCount.doubleValue() / questionCount.doubleValue() * 100;
        questionAnalysisVO.setFinishedRate(finishedRate);

        //4.查询正确率
        //计算总得分
        List<Response> responses = responseMapper.selectList(new QueryWrapper<>());
        List<String> list = responses.stream().map(Response::getType).toList();
        Long Count1 = list.stream().filter(s -> s.equals("faq")).count();
        Long Count2 = list.stream().filter(s -> s.equals("hama")).count();
        Long sum = Count1 * 3 * 10 + Count2 * 4 * 15 ;
        //计算用户得分
        List<ResponseDetails> responseDetails = responseDetailsMapper.selectList(new QueryWrapper<>());
        Long userScore = (long) responseDetails.stream().mapToInt(ResponseDetails::getScore).sum();
        //计算正确率
        Double correctRate = userScore.doubleValue() / sum.doubleValue() * 100;
        questionAnalysisVO.setCorrectRate(correctRate);

        //5.计算平均时间
        List<Response> responseList = responseMapper.selectList(new QueryWrapper<>());
        Double averageTime = responseList.stream().mapToDouble(responseDetail -> {
            LocalDateTime startTime = responseDetail.getStartTime();
            LocalDateTime submitTime = responseDetail.getSubmitTime();
            return TimeUtil.getTimeDifference(startTime, submitTime);
        }).average().orElse(0.0);
        questionAnalysisVO.setAverageTime(averageTime);

        return questionAnalysisVO;
    }

    @Override
    public PageResult PageQuery(PageAnalysisDto pageAnalysisDto) {
        List<PageAnalysisVO> pageAnalysisVOS = questionAnalysisMapper.PageQuery(pageAnalysisDto);
        for (PageAnalysisVO pageAnalysisVO : pageAnalysisVOS) {
            if(pageAnalysisVO.getTitle().equals("faq")){
                pageAnalysisVO.setTitle("社会功能活动问卷");
            } else if (pageAnalysisVO.getTitle().equals("hama")) {
                pageAnalysisVO.setTitle("汉密尔顿焦虑量表");
            }
            pageAnalysisVO.setStatus(1);
            //获取答题时间
            Response response = responseMapper.selectById(pageAnalysisVO.getId());
            LocalDateTime startTime = response.getStartTime();
            LocalDateTime submitTime = response.getSubmitTime();
            TimeUtil timeUtil = new TimeUtil();
            Double costTime = timeUtil.getTimeDifference(startTime, submitTime);
            pageAnalysisVO.setCostTime(costTime);
        }
        // 查询总记录数
        PageResult pageResult = new PageResult(responseMapper.selectCount(new QueryWrapper<>()), pageAnalysisVOS);
        return pageResult;
    }

    @Override
    public List<ResponseDetails> selectDetailsById(Integer id) {
        QueryWrapper<ResponseDetails> detailsQueryWrapper = new QueryWrapper<>();
        detailsQueryWrapper.eq("response_id", id);
        List<ResponseDetails> responseDetails = responseDetailsMapper.selectList(detailsQueryWrapper);
        return responseDetails;
    }
}
